import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import Index from '@/views/Index.vue'
import EditPwd from '@/views/EditPwd.vue'
import Help from '@/views/Help.vue'
import HelpDetail from '@/views/HelpDetail.vue'
import MyAgent from '@/views/MyAgent.vue'
import Me from '@/views/component/Me.vue'
import Goods from '@/views/component/Goods.vue'
import AgentDetail from '@/views/AgentDetail.vue'
import TopUp from '@/views/TopUp.vue'
import Quota from '@/views/Quota.vue'
import Card from '@/views/Card.vue'
import BindCard from '@/views/BindCard.vue'
import UnbindCard from '@/views/UnbindCard.vue'
import GoodsDetail from '@/views/GoodsDetail.vue'
import ProductDetail from '@/views/ProductDetail.vue'
import AgentRecord from '@/views/AgentRecord.vue'
import AgentThat from '@/views/AgentThat.vue'
import Auth from '@/views/Auth.vue'
import FirmAuth from '@/views/FirmAuth.vue'
import UserAuth from '@/views/UserAuth.vue'
import ShowMsg from '@/views/ShowMsg.vue'
import Income from '@/views/Income.vue'
import SignUp from '@/views/SignUp.vue'
import Explain from '@/views/authentication/Explain.vue'
import Personal from '@/views/authentication/Personal.vue'
import Company from '@/views/authentication/Company.vue'
import Contract from '@/views/authentication/Contract.vue'
import TokenSync from '@/views/sso/TokenSync.vue'
import NotFound from '../views/error-page/404'
import Transfer from '../views/capital/transfer'
import Statistics from '../views/capital/statistics'

import { getToken } from "@/utils/auth";
Vue.use(VueRouter)

const routes = [{
  path: '/login',
  name: 'login',
  component: Login,
  meta: {
    title: "登录_云木屋"
  }
},
{
  path: '/signUp',
  name: 'signUp',
  component: SignUp,
  meta: {
    title: "注册_云木屋"
  }
},
{
  path: '/edit_pwd',
  name: 'edit_pwd',
  component: EditPwd,
  meta: {
    title: "忘记密码"
  }
},
{
  path: '/help',
  name: 'help',
  component: Help,
  meta: {
    title: "帮助中心"
  }
},
{
  path: '/help_detail',
  name: 'help_detail',
  component: HelpDetail,
  meta: {
    title: "问题详情"
  }
},

{
  path: '/',
  name: 'index',
  component: Index,
  meta: {
    title: "我的云木屋",
    keepAlive: true
  },
  children: [{
    path: '/',
    component: Me,
    name: 'me',
    meta: {
      title: "我的云木屋",
      keepAlive: true
    }
  },
  {
    path: 'goods',
    component: Goods,
    name: 'goods',
    meta: {
      title: "海量货源",
      keepAlive: true
    }
  },
  ]
},
{
  path: '/my_agent',
  name: 'my_agent',
  component: MyAgent,
  meta: {
    title: "我的代理",
    keepAlive: true
  }
},
{
  path: '/agent_detail',
  name: 'agent_detail',
  component: AgentDetail,
  meta: {
    title: "代理详情"
  }
},
{
  path: '/top_up',
  name: 'top_up',
  component: TopUp,
  meta: {
    title: "快捷充值"
  }
},
{
  path: '/quota',
  name: 'quota',
  component: Quota,
  meta: {
    title: "银行限额"
  }
},
{
  path: '/card',
  name: 'card',
  component: Card,
  meta: {
    title: "我的银行卡"
  }
},
{
  path: '/bind_card',
  name: 'bind_card',
  component: BindCard,
  meta: {
    title: "添加银行卡"
  }
},
{
  path: '/un_bind_card',
  name: 'un_bind_card',
  component: UnbindCard,
  meta: {
    title: "解绑银行卡"
  }
},
{
  path: '/goods_detail',
  name: 'goods_detail',
  component: GoodsDetail,
  meta: {
    title: "货源详情"
  }
},
{
  path: '/product_detail',
  name: 'product_detail',
  component: ProductDetail,
  meta: {
    title: "商品详情"
  }
},
{
  path: '/agent_record',
  name: 'agent_record',
  component: AgentRecord,
  meta: {
    title: "代理记录"
  }
},
{
  path: '/agent_that',
  name: 'agent_that',
  component: AgentThat,
  meta: {
    title: "代理说明"
  }
},
{
  path: '/income',
  name: 'income',
  component: Income,
  meta: {
    title: "我的收入"
  }
},
{
  path: '/showmsg',
  name: 'showmsg',
  component: ShowMsg,
},
{
  path: '/explain',
  component: Explain,
  name: 'explain',
  meta: {
    title: "实名认证"
  },

},

{
  path: '/personal',
  component: Personal,
  name: 'personal',
  meta: {
    title: "个人认证"
  },

},
{
  path: '/company',
  component: Company,
  name: 'company',
  meta: {
    title: "企业认证"
  },

},
{
  path: '/authentication/contractServices',
  component: Contract,
  name: 'contract',
  meta: {
    title: "签署合同"
  },

},
{
  path: '/index/sso/tokenSync',
  component: TokenSync,
  name: 'tokenSync',
  meta: {
    title: "tokenSync"
  },

},
{
  path: '/capital/transfer',
  component: Transfer,
  name: 'transfer',
  meta: {
    title: "钱包余额互转"
  },

},
{
  path: '/capital/statistics',
  component: Statistics,
  name: 'statistics',
  meta: {
    title: "资金统计"
  },

},
{
  path: '/404',
  component: NotFound,
  name: 'NotFound',
  meta: {
    title: "404"
  },

},
{
  path: '*',
  redirect: '/404',
  hidden: true
}
  // {
  //   path: '/auth',
  //   name: 'auth',
  //   component: Auth,
  //   meta: {
  //     title: "实名认证"
  //   }
  // },
  // {
  //   path: '/firm_auth',
  //   name: 'firm_auth',
  //   component: FirmAuth,
  //   meta: {
  //     title: "企业认证"
  //   }
  // },
  // {
  //   path: '/user_auth',
  //   name: 'user_auth',
  //   component: UserAuth,
  //   meta: {
  //     title: "个人认证"
  //   }
  // },
]



const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.path === '/login' || to.path === '/showmsg' || to.path === '/signUp' || to.path === '/edit_pwd' || to.path === '/tokenSync') {
    next()
  } else {
    let token = getToken() || ''
    if (token) {
      next()
    } else {
      next('/login')
    }
  }

})
export default router